package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 表格上传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {

        try {
            //读取上传文件，并返回每行信息的集合
            List<String[]> list = POIUtils.readExcel(multipartFile);
            //判断文件内容是否为空
            if (list != null && list.size() > 0) {
                //创建预约设置集合容器
                List<OrderSetting> orderSettingList = new ArrayList<>();
                //遍历
                for (String[] strings : list) {
                    //将表格中每行的信息封装到预约设置类中
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    //将实体类存放到容器中
                    orderSettingList.add(orderSetting);
                }
                //将封装好的预约设置容器传递给业务层处理
                orderSettingService.add(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败返回
            new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //上传成功返回
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据日期查询预约设置数据
     *
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//2019-06
        try {
            List<Map> map = orderSettingService.getOrderSettingByMonth(date);
            //成功返回
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            //失败返回
            return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据指定日期修改可预约人数
     *
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editOrderSettingByDate")
    public Result editOrderSettingByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editOrderSettingByDate(orderSetting);
            //修改预约人数成功
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改预约人数失败
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
